package com.nanhai.buddhist.college.module.collegebase.service.accommodationfloor;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nanhai.buddhist.college.framework.common.pojo.PageResult;
import com.nanhai.buddhist.college.framework.common.util.collection.CollectionUtils;
import com.nanhai.buddhist.college.framework.mybatis.core.dataobject.BaseDO;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.accommodationfloor.vo.AccommodationFloorCreateReqVO;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.accommodationfloor.vo.AccommodationFloorExportReqVO;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.accommodationfloor.vo.AccommodationFloorPageReqVO;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.accommodationfloor.vo.AccommodationFloorUpdateReqVO;
import com.nanhai.buddhist.college.module.collegebase.convert.accommodationfloor.AccommodationFloorConvert;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.accommodationfloor.AccommodationFloorDO;
import com.nanhai.buddhist.college.module.collegebase.dal.mysql.accommodationfloor.AccommodationFloorMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import static com.nanhai.buddhist.college.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.nanhai.buddhist.college.module.collegebase.enums.ErrorCodeConstants.ACCOMMODATION_FLOOR_NOT_EXISTS;

/**
 * 楼层 Service 实现类
 *
 * @author AKai
 */
@Service
@Validated
public class AccommodationFloorServiceImpl implements AccommodationFloorService {

    @Resource
    private AccommodationFloorMapper accommodationFloorMapper;





    @Override
    public String createAccommodationFloor(AccommodationFloorCreateReqVO createReqVO) {
        // 插入
        AccommodationFloorDO accommodationFloor = AccommodationFloorConvert.INSTANCE.convert(createReqVO);
        accommodationFloorMapper.insert(accommodationFloor);
        // 返回
        return accommodationFloor.getId();
    }

    @Override
    public void updateAccommodationFloor(AccommodationFloorUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateAccommodationFloorExists(updateReqVO.getId());
        // 更新
        AccommodationFloorDO updateObj = AccommodationFloorConvert.INSTANCE.convert(updateReqVO);
        accommodationFloorMapper.updateById(updateObj);
    }

    @Override
    public void deleteAccommodationFloor(String id) {
        // 校验存在
        this.validateAccommodationFloorExists(id);


        // 删除
        accommodationFloorMapper.deleteById(id);
    }

    private void validateAccommodationFloorExists(String id) {
        if (accommodationFloorMapper.selectById(id) == null) {
            throw exception(ACCOMMODATION_FLOOR_NOT_EXISTS);
        }
    }

    @Override
    public AccommodationFloorDO getAccommodationFloor(String id) {
        return accommodationFloorMapper.selectById(id);
    }

    @Override
    public List<AccommodationFloorDO> getAccommodationFloorList(Collection<String> ids) {
        return CollectionUtils.isEmpty(ids) ? Collections.emptyList() : accommodationFloorMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<AccommodationFloorDO> getAccommodationFloorPage(AccommodationFloorPageReqVO pageReqVO) {
        return accommodationFloorMapper.selectPage(pageReqVO);
    }

    @Override
    public List<AccommodationFloorDO> getAccommodationFloorList(AccommodationFloorExportReqVO exportReqVO) {
        return accommodationFloorMapper.selectList(exportReqVO);
    }

    @Override
    public List<AccommodationFloorDO> getAccommodationFloorByBuildId(String buildId) {
        return accommodationFloorMapper.selectList(AccommodationFloorDO::getBuildingId, buildId);
    }

    @Override
    public List<AccommodationFloorDO> getAccommodationFloorByBuildingList(String id) {
        LambdaQueryWrapper<AccommodationFloorDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AccommodationFloorDO::getBuildingId, id)
                .orderByAsc(BaseDO::getCreateTime);
        return accommodationFloorMapper.selectList(wrapper);
    }

}
